import request from "@/services/interceptors";
import { AddEditProps } from "@/types";
import { PlusOutlined } from "@ant-design/icons";
import { ModalForm, ProFormDigit } from "@ant-design/pro-components";
import { Button, Form } from "antd";
import { useState } from "react";

const ScoreEdit = ({ editData, onSuccess, trigger }: AddEditProps) => {
  const [form] = Form.useForm<{ score?: number }>();
  const [exerciseSteps, setExerciseSteps] = useState([]);
  const onOpenChange = async (open: boolean) => {
    if (open) {
      // 客观题
      if (editData?.questionId) {
        form.setFieldsValue({ score: editData?.score || 0 });
      } else {
        // 操作题
        if (editData?.exerciseSteps?.length) {
          form.setFieldsValue({
            score: editData?.exerciseSteps[0]?.exerciseScore,
          });
          setExerciseSteps(editData?.exerciseSteps);
        } else {
          const res: any = await request.sgks.exerciseGetStepsList({
            exerciseId: editData?.exerciseId,
          });
          setExerciseSteps(res.data);
          form.setFieldsValue({
            score: res.data?.exerciseSteps[0]?.exerciseScore ?? 0,
          });
        }
      }
    }
  };
  return (
    <ModalForm<{
      score?: number;
    }>
      title={"编辑题目分数"}
      layout="horizontal"
      trigger={
        trigger || (
          <Button type="primary" icon={<PlusOutlined />}>
            新增
          </Button>
        )
      }
      onOpenChange={onOpenChange}
      form={form}
      autoFocusFirstInput
      modalProps={{
        width: 800,
        destroyOnClose: true,
      }}
      onFinish={async (values) => {
        onSuccess?.({ ...values, exerciseSteps });
        return true;
      }}
    >
      <ProFormDigit
        label="分数"
        name="score"
        min={1}
        max={100}
        rules={[{ required: true }]}
        fieldProps={{ precision: 0 }}
      />
    </ModalForm>
  );
};

export default ScoreEdit;
